﻿using Basic.FlowDAL;
using Basic.FlowModel.AuditState;
using Basic.FlowRemoteModel;

namespace Basic.FlowCollect.lmpl
{
    internal class FlowAuditNodeStateCollect : IFlowAuditNodeStateCollect
    {
        private readonly IFlowAuditNodeStateDAL _BasicDAL;

        public FlowAuditNodeStateCollect ( IFlowAuditNodeStateDAL stateDAL )
        {
            this._BasicDAL = stateDAL;
        }

        public void SetJumpNode ( long logId, long[] nodeId )
        {
            this._BasicDAL.SetJumpNode(logId, nodeId);
        }
        public Dictionary<long, FlowNodeAuditStatus> GetNodeStatus ( long logId )
        {
            return this._BasicDAL.Gets(a => a.LogId == logId, a => new
            {
                a.NodeId,
                a.Status
            }).ToDictionary(a => a.NodeId, a => a.Status);
        }
        public Dictionary<long, FlowNodeAuditStatus> GetNodeStatus ( long logId, long rootNodeId )
        {
            return this._BasicDAL.Gets(a => a.LogId == logId && a.RootNodeId == rootNodeId, a => new
            {
                a.NodeId,
                a.Status
            }).ToDictionary(a => a.NodeId, a => a.Status);
        }
        public AuditNodeState[] GetNodeStatus ( long logId, long[] nodeId )
        {
            return this._BasicDAL.Gets<AuditNodeState>(a => a.LogId == logId && nodeId.Contains(a.NodeId));
        }

        public long[] GetGoByNodeId ( long logId, long rootNodeId )
        {
            FlowNodeAuditStatus[] status = new FlowNodeAuditStatus[]
            {
                FlowNodeAuditStatus.审核通过,
                FlowNodeAuditStatus.审核进行中
            };
            return this._BasicDAL.Gets(a => a.LogId == logId && a.RootNodeId == rootNodeId && status.Contains(a.Status), a => a.NodeId);
        }

        public AuditNodeState[] GetRunNode ( long logId )
        {
            FlowNodeAuditStatus[] status = new FlowNodeAuditStatus[]
            {
                FlowNodeAuditStatus.已跳过,
                FlowNodeAuditStatus.审核通过,
                FlowNodeAuditStatus.审核进行中,
                FlowNodeAuditStatus.终审通过,
                FlowNodeAuditStatus.审核未通过
            };
            return this._BasicDAL.Gets<AuditNodeState>(a => a.LogId == logId && status.Contains(a.Status));
        }

        public RestoreNodeState[] GetRestoreNode ( long id, long optItemId )
        {
            return this._BasicDAL.Gets<RestoreNodeState>(a => a.LogId == id && a.OptItemId != null && a.OptItemId == optItemId);
        }
    }
}
